package com.project.personnel.controller;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.personnel.entity.*;
import com.project.personnel.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 员工离职
 */
@RestController
@RequestMapping("/employeeretire")
public class EmployeeretireController {
    /**
     * 员工信息
     */
    @Autowired
    private EmployeeService employeeService;
    /**
     * 员工离职
     */
    @Autowired
    private EmployeeretireService employeeretireService;
    /**
     * 员工培训
     */
    @Autowired
    private EmployeetrainService employeetrainService;
    /**
     * 员工调动
     */
    @Autowired
    private EmployeeremoveService employeeremoveService;
    /**
     * 员工调薪
     */
    @Autowired
    private AdjustsalaryService adjustsalaryService;
    /**
     * 员工奖惩
     */
    @Autowired
    private EmployeeecService employeeecService;

    /**
     * 员工离职全部功能
     * @param employeeretire
     * @return
     */
    @RequestMapping("/dimission")
    private Integer dimission(Employeeretire employeeretire){
        // 创建一个当前离职时间
        employeeretire.setRetireDate(new Date());
        // 保存员工离职对象
        boolean save = employeeretireService.save(employeeretire);
        // 根据员工ID来删除员工信息,0是成功，1是失败
        if (save){
            /**
             * 删除关联员工培训信息
             */
            // 1.创建员工培训信息对象
            Employeetrain employeetrain = new Employeetrain();
            // 2.设置员工id
            employeetrain.setEid(employeeretire.getEid());
            // 3.创建根据员工id删除信息的条件对象Wrapper
            Wrapper<Employeetrain> employeetrainWrapper = new QueryWrapper<>(employeetrain);
            // 4.根据条件对象删除关联员工培训信息
            employeetrainService.remove(employeetrainWrapper);

            /**
             * 删除关联员工调动信息
             */
            // 1.创建员工调动信息对象
            Employeeremove employeeremove = new Employeeremove();
            // 2.设置员工id
            employeeremove.setEid(employeeretire.getEid());
            // 3.创建根据员工id删除信息的条件对象Wrapper
            Wrapper<Employeeremove> employeeremoveWrapper = new QueryWrapper<>(employeeremove);
            // 4.根据条件对象删除关联员工调动信息
            employeeremoveService.remove(employeeremoveWrapper);

            /**
             * 删除关联员工调薪信息
             */
            // 1.创建员工调薪信息对象
            Adjustsalary adjustsalary = new Adjustsalary();
            // 2.设置员工id
            adjustsalary.setEid(employeeretire.getEid());
            // 3.创建根据员工id删除信息的条件对象Wrapper
            Wrapper<Adjustsalary> adjustsalaryWrapper = new QueryWrapper<>(adjustsalary);
            // 4.根据条件对象删除关联员工调薪信息
            adjustsalaryService.remove(adjustsalaryWrapper);

            /**
             * 删除关联员工奖惩信息
             */
            // 1.创建员工奖惩信息对象
            Employeeec employeeec=new Employeeec();
            // 2.设置员工id
            employeeec.setEid(employeeretire.getEid());
            // 3.创建根据员工id删除信息的条件对象Wrapper
            Wrapper<Employeeec> wrapper=new QueryWrapper(employeeec);
            // 4.根据条件对象删除关联员工奖惩信息
            employeeecService.remove(wrapper);

            /**
             * 更新员工信息
             */
            // 创建员工对象
            Employee employee = new Employee();
            // 赋值员工对象ID
            employee.setId(employeeretire.getEid());
            // 赋值员工离职日期
            employee.setNotWorkDate(employeeretire.getRetireDate());
            // 修改任职状态
            employee.setWorkState("离职");
            // 更新员工在职信息
            boolean b = employeeService.updateById(employee);
            if (b){
                return 0;
            }else {
                return 1;
            }
        }else {
            return 1;
        }
    }
}
